import Vue from 'vue';
import VueRouter from 'vue-router';
import HomeView from '../views/HomeView.vue';
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter);

const routes = [
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    meta: {
      title: '404',
      requireAuth: false,//添加该字段，表示进入路由是否需要登录
    },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/Login'),
    meta: {
      title: '登录',
      requireAuth: false,//添加该字段，表示进入路由是否需要登录
    },
  },
  {
    path: '/',
    name: '',
    component: HomeView,
    redirect: '/',//路由重定向
    meta: {
      title: '首页',
      requireAuth: false,//添加该字段，表示进入路由是否需要登录
    },
    children: [
      {
        path: '/',
        name: 'home',
        component: () => import('@/views/homePage/index'),
        meta: {
          title: '首页',
          requireAuth: false
        }
      },
      // {
      //   path: '/community',
      //   name: 'community',
      //   component: () => import('@/views/community/index'),
      //   meta: {
      //     title: '社区',
      //     requireAuth: false
      //   }
      // },
      {
        path: '/dynamic',
        name: 'dynamic',
        component: () => import('@/views/dynamic/index'),
        meta: {
          title: '动态',
          requireAuth: false
        }
      },
      {
        path: '/dynamic/dynamicDetails',
        name: 'dynamicDetails',
        component: () => import('@/views/dynamic/modules/dynamicDetails'),
        meta: {
          title: '动态详情',
          requireAuth: false
        }
      },
      {
        path: '/privacy',
        name: 'privacy',
        component: () => import('@/views/dynamic/modules/privacy'),
        meta: {
          title: '隐私服务',
          requireAuth: false
        }
      },
      {
        path: '/agreement',
        name: 'agreement',
        component: () => import('@/views/dynamic/modules/agreement'),
        meta: {
          title: '用户协议',
          requireAuth: false
        }
      },
      {
        path: '/manager',
        name: 'manager',
        component: () => import('@/views/manager/index'),
        meta: {
          title: '申请管理员',
          requireAuth: true
        }
      },
      {
        path: '/announcement',
        name: 'announcement',
        component: () => import('@/views/manager/announcement'),
        meta: {
          title: '社区中心',
          requireAuth: false
        }
      },
      {
        path: '/userSettings',
        name: 'userSettings',
        component: () => import('@/views/userSettings/index'),
        meta: {
          title: '用户设置',
          requireAuth: true,//添加该字段，表示进入路由是否需要登录
        },
      }
    ]
  },
];

const router = new VueRouter({
  mode: process.env.VUE_APP_ROUTER_MODE,
  base: process.env.BASE_URL,
  routes,
});
// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.matched.length) {//路由是否存在，不存在跳转404
    if (to.meta.requireAuth) { //是否需要登录
      // 是否已登录
      let token = window.localStorage.getItem('token');
      if (token) {
        //如果用户信息存在则往下执行。
        // 登录状态下去登录页 直接跳转首页

        if (to.path === '/login') {
          next('/')
        }
        next()
      } else {
        //如果用户token不存在则跳转到login页面
        if (to.path === '/login') {
          next()
        } else {
          next('/login')
        }
      }
    } else {
      let token = window.localStorage.getItem('token');
      if (token) {
        // 登录状态下去登录页 直接跳转首页
        if (to.path === '/login') {
          next('/')
        }
        next()
      }
      next()
    }
  } else {
    next('/404')
  }
})
export default router;
